//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.zl.framework.configs;

import com.alibaba.fastjson.JSON;
import com.zl.framework.scm.client.SCMConfiguration;
import com.zl.framework.util.CommonConfig;
import com.zl.framework.util.DBConstant;

import java.lang.reflect.Field;
import java.util.Properties;

import com.zl.init.SSHDBUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

@Order(Integer.MIN_VALUE)
@Component
public class XLSystemPropertyConfigurer extends PropertyPlaceholderConfigurer {
    private final Logger logger = LoggerFactory.getLogger(XLSystemPropertyConfigurer.class);

    public XLSystemPropertyConfigurer()
    {

        ClassPathResource[] resources = new ClassPathResource[]{
                new ClassPathResource("basicclient.properties"),
                new ClassPathResource("setting-web.properties"),
                new ClassPathResource("entryclient.properties")
        };

        setLocations(resources);
    }

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
    {
        logger.info("读取系统配置-start");
        if (!DBConstant.properDb.isEmpty()) {
            props.putAll(DBConstant.properDb);
        }
        logger.info("读取系统配置-end");

        logger.info("替换MySql配置-start");
        String[] propertiekeys = {"dataSourceDefault.url", "dataSource1.url", "dataSource2.url",
                "dataSourceDefaultS.url", "dataSource1S.url", "dataSource2S.url"};
        int lport = 3306;
        for (String propertiekey : propertiekeys) {
            try {
                String url = props.getProperty(propertiekey);
                String[] arrays = url.split("/");
                String ip = arrays[2].split(":")[0];
                String rport = arrays[2].split(":")[1];
                url = url.replace(ip, "127.0.0.1").replace(rport, String.valueOf(++lport));
                SSHDBUtil.initDB(ip, Integer.valueOf(rport), lport);
                props.setProperty(propertiekey, url);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        logger.info("替换MySql配置-end");
        logger.info("修罗替换之后的props:{}", JSON.toJSONString(props));
        //替换配置
        super.processProperties(beanFactoryToProcess, props);
    }
}
